package de.cbf.cam.map2d;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import de.cbf.cam.map2d.interfaces.SelectableShape;
import de.cbf.cam.map2d.map.Map;
import de.cbf.cam.map2d.model.Model;
import de.cbf.cam.map2d.shapes.Shape;
import de.cbf.cam.map2d.views.ViewMap2D;

/**
 * The activator class controls the plug-in life cycle
 */
public class ActivatorMap2D extends AbstractUIPlugin {
	// The plug-in ID
	public static final String PLUGIN_ID = "de.cbf.cam.map2D";
	// The shared instance
	private static ActivatorMap2D plugin;

	private ViewMap2D view;
	/**
	 * The constructor
	 */
	public ActivatorMap2D() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static ActivatorMap2D getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	public void setView(ViewMap2D viewMap2D) {
		this.view=viewMap2D;
	}
	
	public Map getMap(){
		return view.getMap();
	}
	
	public void selectObject(){
		Canvas canvas =getMap();
		//handle selection
		List<SelectableShape> selected = new ArrayList<SelectableShape>();
		Point point = getMap().getMousePosition();
		for(int i =0;i<Model.getInstance().getSelectableObjects().size();i++){
			SelectableShape selectable = Model.getInstance().getSelectableObjects().get(i).checkCollision(point);
			if(selectable!=null){
				selected.add(selectable);
			}
		}			
		for(int i=0;i<selected.size();i++){
			//do actions for selected
			selected.get(i).getSelectionListener().onSelection(canvas, selected.get(i).getShape());
		}
	}
	
	
	public void addObject(Shape shape){
		view.addShape(shape);
		if (shape instanceof SelectableShape) {
			SelectableShape selectableObject = (SelectableShape) shape;
			Model.getInstance().addSelectableObject(selectableObject);
		}
	}
	
	public boolean removeObject(Shape object){
		if (object instanceof SelectableShape) {
			SelectableShape selectableObject = (SelectableShape) object;
			return Model.getInstance().removeSelectableObject(selectableObject);
		}
		return true;
	}
	
	
}
